import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'

import tailwindcss from '@tailwindcss/vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

export default defineConfig(({ mode }) => {
  const { TITLE, BASE_URL = '' } = loadEnv(mode, '../', '')

  return {
    base: `${BASE_URL}/`,
    plugins: [vue(), vueDevTools(), tailwindcss()],
    server: {
      port: 5175,
      proxy: {
        [`${BASE_URL}/api`]: {
          target: 'http://127.0.0.1:8001',
          changeOrigin: true,
          rewrite: path => path.replace(new RegExp(`^${BASE_URL}/api`), '')
        }
      }
    },
    envDir: '../',
    define: {
      'import.meta.env.TITLE': JSON.stringify(TITLE)
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    }
  }
})
